-- 植物类
local Object = require("app.mode.Object")
local AniHelper = require("app.utils.AnimateHelper")

local Plant = {}
Plant.__index = Plant
setmetatable(Plant, Object)

-- 每条轨道的僵尸数量（静态变量）
Plant._z_counts = {0, 0, 0, 0, 0}

function Plant:initView()
    self._sprite, self._animate = AniHelper("plants/", self:getName())
end

function Plant:initData(data)
    Object.initData(self, data)
    -- 攻击目标
    self._target = {}
end

function Plant:exportData()
    local data = Object.exportData(self)
    data.logic_x, data.logic_y = self:getLogicPosition()
    return data
end

-- 开始功能循环
function Plant:startFuncLoop()
    local function update(delta)
        if isPause then
            return
        end

        self:setTime(self:getTime() + 0.5)

        if self:getTime() >= self:getFuncWaitTime() then
            -- 执行功能函数
            self:doFunc()
            self:setTime(0)
        end
    end

    self._timer = cc.Director:getInstance():getScheduler():scheduleScriptFunc(update, 0.5, false)
end

-- 停止功能循环
function Plant:stopLoop()
    if self._timer then
        cc.Director:getInstance():getScheduler():unscheduleScriptEntry(self._timer)
        self._timer = nil
    end
end

-- 功能函数
function Plant:doFunc()
end

-- 被攻击
function Plant:beAttack(attack)
    self:setBlood(self:getBlood() - attack)
    -- 死亡
    if self:getBlood() <= 0 then
        self:preRemove()
        self:removeSelf()
    end
end

-- 移除数据
function Plant:preRemove()
    -- 移除僵尸的攻击目标
    for _, z in ipairs(self:getTarget()) do
        z:unAttack()
    end
    -- 移除自身目标
    self._target = nil
    self._scene:removePlant(self)
end

-- 界面显示相关
function Plant:setLogicPosition(x, y)
    self._data.logicX, self._data.logicY = x, y
    self:setPosition(win_size.width / 2 + (x - 5) * 80, win_size.height / 2 - 20 + (y - 3) * 100)
    self:setDepth(5 - y)
end

function Plant:getLogicPosition()
    return self._data.logicX, self._data.logicY
end

function Plant:getZCount(track)
    return Plant._z_counts[track]
end

function Plant:setZCount(track, val)
    Plant._z_counts[track] = val
end

function Plant:resetZCount()
    self._z_counts = {0, 0, 0, 0, 0}
end

function Plant:addZombie(track)
    Plant._z_counts[track] = Plant._z_counts[track] + 1
end

function Plant:removeZombie(track)
    Plant._z_counts[track] = Plant._z_counts[track] - 1
end

function Plant:addTarget(t)
    table.insert(self._target, t)
end

function Plant:removeTarget(t)
    local inx = table.indexof(self._target, t)
    if inx then
        table.remove(self._target, inx)
    end
end

function Plant:getTarget()
    return self._target
end

-- 数据
function Plant:getNeedSunShine()
    return self._data.need_sun_shine
end

function Plant:getNewWaitTime()
    return self._data.new_wait_time
end

function Plant:getFuncWaitTime()
    return self._data.func_wait_time
end

function Plant:getMaxBlood()
    return self._data.max_blood
end

function Plant:getBlood()
    return self._data.blood
end

function Plant:setBlood(val)
    self._data.blood = val
end

function Plant:getTime()
    return self._data.time
end

function Plant:setTime(val)
    self._data.time = val
end

return Plant
